package com.crm.miaohe.service;

import com.crm.miaohe.common.ApplicationConstant;
import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.information.request.InformationQueryRequest;
import com.crm.miaohe.controller.information.request.InformationRequest;
import com.crm.miaohe.exception.ErpBusinessException;
import com.crm.miaohe.mapper.TblInformationMapper;
import com.crm.miaohe.model.TblInformation;
import com.crm.miaohe.model.TblInformationExample;
import com.crm.miaohe.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Slf4j
@Service
public class InformationServiceImpl implements InformationService {
    private final TblInformationMapper informationMapper;

    public InformationServiceImpl(TblInformationMapper informationMapper) {
        this.informationMapper = informationMapper;
    }

    @Override
    public void addInformation(InformationRequest request) {
        TblInformation information = new TblInformation();
        BeanUtils.copyProperties(request, information);
        informationMapper.insertSelective(information);
    }

    @Override
    public void updateInformation(InformationRequest request) {
        if (Objects.isNull(request.getId())) {
            log.error("请求参数[{}]缺失，请求终止。", "information.id");
            throw new ErpBusinessException(HttpStatus.BAD_REQUEST, ApplicationConstant.REQUEST_PARAM_INVALID, "id");
        }
        TblInformation information = Optional.ofNullable(informationMapper.selectByPrimaryKey(request.getId()))
                .orElseThrow(() -> {
                    log.error("数据未找到，更新失败。ID：{}", request.getId());
                    throw new ErpBusinessException(HttpStatus.BAD_REQUEST, ApplicationConstant.DATA_NOT_FOUND,
                            "information", request.getId());
                });
        information.setTitle(request.getTitle());
        information.setSummary(request.getSummary());
        information.setImage(request.getImage());
        information.setContent(request.getContent());
        information.setThumb(request.getThumb());
        informationMapper.updateByPrimaryKeySelective(information);
    }

    @Override
    public void delete(Long informationId) {
        informationMapper.deleteByPrimaryKey(informationId);
    }

    @Override
    public void delete(List<Long> informationIds) {
        TblInformationExample example = new TblInformationExample();
        example.createCriteria().andIdIn(informationIds);
        informationMapper.deleteByExample(example);
    }

    @Override
    public PageResponse<TblInformation> queryInformation(InformationQueryRequest request) {
        TblInformationExample example = new TblInformationExample();
        example.createCriteria().andTitleLike(StringUtils.quoteWith(request.getTitle(), "%"));
        example.setOrderByClause("create_time desc");
        PageInfo<TblInformation> pageInfo = PageHelper.startPage(request.getPageNo(), request.getPageSize())
                .doSelectPageInfo(() -> informationMapper.selectByExample(example));
        return PageResponse.<TblInformation>builder()
                .records(pageInfo.getList())
                .currentPage(pageInfo.getPageNum())
                .totalElement(pageInfo.getTotal())
                .totalPage(pageInfo.getPages())
                .build();
    }

    @Override
    public TblInformation queryInformation(Long informationId) {
        return Optional.ofNullable(informationMapper.selectByPrimaryKey(informationId))
                .orElseThrow(() -> {
                    log.error("Data Not Found for TblInformation[{}]", informationId);
                    throw new ErpBusinessException(HttpStatus.NOT_FOUND, ApplicationConstant.DATA_NOT_FOUND,
                            "information", informationId);
                });
    }
}
